package com.lv.service.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lv.common.bean.CommonSelectedBean
import com.lv.common.imp.OnPositionTListener
import com.lv.common.imp.OnPositionTMore
import com.lv.service.R
import com.lv.service.adapter.TextTagSelectedMoreAdapter
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class Recycler3SelectedPopupView(
    private val mContext: Context,
    private val mTitle: String,
    private val mFristBeans: MutableList<CommonSelectedBean>,
    private val mSecoundBeans: MutableList<CommonSelectedBean>,
    private val mProvinceBeans: MutableList<CommonSelectedBean>,
    private var topH: Int = 0,
    private val onlyOneEnable: Boolean = false,
    private val mLayout: Int = R.layout.recycler3_selected_popup,
    private val mPositionMListener: OnPositionTMore<List<CommonSelectedBean>>? = null
) :
    BottomPopupView(mContext) {
    private var rvTop: RecyclerView? = null
    private var rvBottom: RecyclerView? = null
    private var rvCenter: RecyclerView? = null
    private var tvTitle: TextView? = null
    private var tvTitleTop: TextView? = null
    private var tvTitleBottom: TextView? = null
    private var ivClose: ImageView? = null
    private var viewTop: View? = null
    private var viewShadow: TextView? = null
    private var mTopBeans = mutableListOf<CommonSelectedBean>()
    private var mCenterBeans = mutableListOf<CommonSelectedBean>()
    private var mBottomBeans = mutableListOf<CommonSelectedBean>()

    private var mTopAdapter: TextTagSelectedMoreAdapter? = null
    private var mCenterAdapter: TextTagSelectedMoreAdapter? = null
    private var mBottomAdapter: TextTagSelectedMoreAdapter? = null
    override fun getImplLayoutId(): Int {
        return mLayout
    }

    override fun onCreate() {
        super.onCreate()
        rvTop = findViewById<RecyclerView>(R.id.rv_top)
        rvCenter = findViewById<RecyclerView>(R.id.rv_center)
        rvBottom = findViewById<RecyclerView>(R.id.rv_bottom)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        tvTitleTop = findViewById<TextView>(R.id.tv_title_1)
        tvTitleBottom = findViewById<TextView>(R.id.tv_title_2)
        viewTop = findViewById<View>(R.id.view_top)
        viewShadow = findViewById<TextView>(R.id.tv_submit)
        ivClose = findViewById<ImageView>(R.id.iv_close)
        val layoutParams = viewTop?.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.height = topH
        initView()
        initAdapter()
        initEvnet()
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initAdapter() {
        mTopBeans.clear()
        mBottomBeans.clear()
        mFristBeans.forEach {
            mTopBeans.add(CommonSelectedBean(it.name, it.tag, it.selected))
        }
        mSecoundBeans.forEach {
            mBottomBeans.add(CommonSelectedBean(it.name, it.tag, it.selected))
        }
        mProvinceBeans.forEach {
            mCenterBeans.add(CommonSelectedBean(it.name, it.tag, it.selected))
        }


        val gridLayoutManager = object : GridLayoutManager(context, 4) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rvTop?.layoutManager = gridLayoutManager
        mTopAdapter = TextTagSelectedMoreAdapter(mTopBeans)
        rvTop?.adapter = mTopAdapter
        mTopAdapter?.onViewClickListener = OnPositionTListener { position, data ->
            if (onlyOneEnable) {
                val index = mTopBeans.indexOfFirst { it.selected }
                if (index == -1) {
                    val selectedPosition = mTopBeans[position].selected
                    mTopBeans[position].selected = !selectedPosition
                    mTopAdapter?.notifyItemChanged(position, R.id.tv_name)
                } else {
                    if (index == position) {
                        val selectedPosition = mTopBeans[position].selected
                        mTopBeans[position].selected = !selectedPosition
                        mTopAdapter?.notifyItemChanged(position, R.id.tv_name)
                    } else {
                        mTopBeans[index].selected = false
                        mTopAdapter?.notifyItemChanged(index, R.id.tv_name)
                        mTopBeans[position].selected = true
                        mTopAdapter?.notifyItemChanged(position, R.id.tv_name)
                    }
                }
            } else {
                val selected = mTopBeans[0].selected
                if (position == 0) {
                    mTopBeans.forEach { item -> item.selected = false }
                    mTopBeans[0].selected = !selected
                    mTopAdapter?.notifyDataSetChanged()
                } else {
                    if (selected) {
                        mTopBeans[0].selected = false
                        mTopAdapter?.notifyItemChanged(0, R.id.tv_name)
                    }
                    val selectedPosition = mTopBeans[position].selected
                    mTopBeans[position].selected = !selectedPosition
                    mTopAdapter?.notifyItemChanged(position, R.id.tv_name)
                }
            }
        }
        val gridLayoutManagerCenter = object : GridLayoutManager(context, 4) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rvCenter?.layoutManager = gridLayoutManagerCenter
        mCenterAdapter = TextTagSelectedMoreAdapter(mCenterBeans)
        rvCenter?.adapter = mCenterAdapter
        mCenterAdapter?.onViewClickListener = OnPositionTListener { position, data ->
            if (onlyOneEnable) {
                val index = mCenterBeans.indexOfFirst { it.selected }
                if (index == -1) {
                    val selectedPosition = mCenterBeans[position].selected
                    mCenterBeans[position].selected = !selectedPosition
                    mCenterAdapter?.notifyItemChanged(position, R.id.tv_name)
                } else {
                    if (index == position) {
                        val selectedPosition = mCenterBeans[position].selected
                        mCenterBeans[position].selected = !selectedPosition
                        mCenterAdapter?.notifyItemChanged(position, R.id.tv_name)
                    } else {
                        mCenterBeans[index].selected = false
                        mCenterAdapter?.notifyItemChanged(index, R.id.tv_name)
                        mCenterBeans[position].selected = true
                        mCenterAdapter?.notifyItemChanged(position, R.id.tv_name)
                    }
                }
            } else {
                val selected = mCenterBeans[0].selected
                if (position == 0) {
                    mCenterBeans.forEach { item -> item.selected = false }
                    mCenterBeans[0].selected = !selected
                    mCenterAdapter?.notifyDataSetChanged()
                } else {
                    if (selected) {
                        mCenterBeans[0].selected = false
                        mCenterAdapter?.notifyItemChanged(0, R.id.tv_name)
                    }
                    val selectedPosition = mCenterBeans[position].selected
                    mCenterBeans[position].selected = !selectedPosition
                    mCenterAdapter?.notifyItemChanged(position, R.id.tv_name)
                }
            }
        }
        val gridLayoutManagerBottom = object : GridLayoutManager(context, 4) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rvBottom?.layoutManager = gridLayoutManagerBottom
        mBottomAdapter = TextTagSelectedMoreAdapter(mBottomBeans)
        rvBottom?.adapter = mBottomAdapter
        mBottomAdapter?.onViewClickListener = OnPositionTListener { position, data ->
            if (onlyOneEnable) {
                val index = mBottomBeans.indexOfFirst { it.selected }
                if (index == -1) {
                    val selectedPosition = mBottomBeans[position].selected
                    mBottomBeans[position].selected = !selectedPosition
                    mBottomAdapter?.notifyItemChanged(position, R.id.tv_name)
                } else {
                    if (index == position) {
                        val selectedPosition = mBottomBeans[position].selected
                        mBottomBeans[position].selected = !selectedPosition
                        mBottomAdapter?.notifyItemChanged(position, R.id.tv_name)
                    } else {
                        mBottomBeans[index].selected = false
                        mBottomAdapter?.notifyItemChanged(index, R.id.tv_name)
                        mBottomBeans[position].selected = true
                        mBottomAdapter?.notifyItemChanged(position, R.id.tv_name)
                    }
                }
            } else {
                val selected = mBottomBeans[0].selected
                if (position == 0) {
                    mBottomBeans.forEach { item -> item.selected = false }
                    mBottomBeans[0].selected = !selected
                    mBottomAdapter?.notifyDataSetChanged()
                } else {
                    if (selected) {
                        mBottomBeans[0].selected = false
                        mBottomAdapter?.notifyItemChanged(0, R.id.tv_name)
                    }
                    val selectedPosition = mBottomBeans[position].selected
                    mBottomBeans[position].selected = !selectedPosition
                    mBottomAdapter?.notifyItemChanged(position, R.id.tv_name)
                }
            }
        }
    }

    private fun initView() {
        tvTitle?.text = mTitle

    }

    private fun initEvnet() {
        viewTop?.setOnClickListener {
            dismiss()
        }
        ivClose?.setOnClickListener {
            dismiss()
        }

        viewShadow?.setOnClickListener {
            mPositionMListener?.onClick(0, mTopBeans,mCenterBeans, mBottomBeans)
            dismiss()
        }
    }

    fun setTitleTop(text: String): Recycler3SelectedPopupView {
        tvTitleTop?.text = text
        return this
    }

    fun setTitleBottom(text: String): Recycler3SelectedPopupView {
        tvTitleBottom?.text = text
        return this
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }

}